<template>
<div>
  <Datalist ref="datalist" :cate="cate" :name="name" :cols="cols" :excelHeader="excelHeader" skipExcelNumber="3" >
   
  </Datalist>
  </div>
</template>
<script>
import Datalist from "@/components/DataList";
import { getUser } from "@/utils/auth";
import linq from "linq"
import {search教师基本情况,get教师基本情况ByGH,get教师基本情况} from "@/api/教师基本情况"
import { add机构设置, delete机构设置, update机构设置, get机构设置,bulkAddJGSZ } from "@/api/机构设置.js";
import {findComponent} from "@/utils/ui";
export default {
  components: {
    Datalist
  },
  data() {
    return {
      cate: "A1-6机构设置表",
     name:'1.6 机构设置',
      excelHeader:{B3:"机构代码",C3:"机构名称(全称)",D3:"负责人",G3:"教职工数（人）",D4:"教工号",E4:"姓名",F4:"任职日期（年月）",G4:"专职",H4:"兼职"},
      cols: [
        { label: "机构代码", prop: "BMBH" },
        { label: "机构名称(全称)", prop: "BMMC" },
        {
          label: "负责人",
          data: [
            { label: "教工号", prop: "GH" },
            { label: "姓名", prop: "XM" },
            { label: "任职日期(年月)", prop: "RZRQ" }
          ]
        },
        {
          label: "教职工数(人)",
          data: [
            { label: "专职", prop: "JZGS_ZZ" },
            { label: "兼职", prop: "JZGS_JZ" }
          ]
        }
      ]
    };
  },
  methods: {
    search(){
      //
      
      //
    },
    getSummaries(list){
      //
        const sums = [];
         this.cols.forEach((column, index) => {
            sums[index]="";
        });
         sums[0]="合计";
         sums[6]= "机构数:"+linq.from(list).count("$.BMMC")
          sums[9]= "领导数:"+linq.from(list).distinct("$.GH").count();
         sums[11]= "专职:"+linq.from(list).sum("$.JZGS_ZZ");
          sums[12]= "兼职:"+linq.from(list).sum("$.JZGS_JZ");
          sums[13]= "通过:"+linq.from(list).where("$.审核状态=='通过'").count();
            sums[13] += ",待审核:"+linq.from(list).where("$.审核状态=='待审核'").count();
       return sums;
      //
    },

    async  onChange(model, field, value) {
      
       if(field == "GH"){
         if(value == "error"){
           console.log("error")
          model.XM=""
         }else{     
          var teachers = await get教师基本情况ByGH(this.$root.user.XXID,this.$root.user.CJPC, value);
           console.log("udpate ")         
          model.XM = teachers[0].XM;
         }
       }
      //
    },
      async  bulkAdd(array){
       await bulkAddJGSZ({JGSZ:array});
    },
    check(array,list) {
      console.log(array,list)
      return new Promise(async (resolve,reject)=>{
         try{
          var user = getUser();
          var 教工号 = await get教师基本情况(user.XXID,user.CJPC)
      
          // var duplicates =  linq.from(array).groupBy("$.GH").where("$.count()>1").toArray();
          // duplicates.forEach(e=>{
          //   linq.from(array).where(t=>t.GH==e.key()).forEach(t=>t.error="录入数据存在相同的教工号");
          // })
          //是否已经存在数据
    
          //是否已经存在相同
  
           var   err =  linq.from(array).select("$.GH").except(linq.from(教工号).select("$.GH").toArray()).toArray();
          linq.from(array).where(e=>{
             
            if(e.GH)
            return err.indexOf(e.GH) !==-1
            else 
            return false;
            }).forEach(e=>e.error=e.error?e.error+";":''+"教师基本情况表教师基本情况表不存在对应的教工号");

  
          //把错误的教师姓名替代掉
          err =  linq.from(array).select("$.GH").except(linq.from(教工号).select("$.GH").toArray()).toArray();
          console.log(err)
          linq.from(array).where(e=>err.indexOf(e.GH+','+e.XM) !==-1).
          forEach(e=>{
            var d = linq.from(教工号).firstOrDefault(item=>item.GH == e.GH);
            if(d){
              e.XM = d.XM;
            }
          });
          var 机构设置 = await get机构设置(user.XXID,user.CJPC)
          var duplicates =  linq.from(array).groupBy("$.BMBH").where("$.count()>1").toArray();
          duplicates.forEach(e=>{
            linq.from(array).where(t=>t.BMBH==e.key()).forEach(t=>t.error="录入数据存在相同的机构代码");
          })
          //是否已经存在数据
        
          var  err =  linq.from(array).select("$.BMBH").intersect(linq.from(list).select("$.BMBH").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.BMBH) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"已存在对应的机构代码");
          
        //把错误的机构名称替代掉
          err =  linq.from(array).select("$.BMBH").except(linq.from(机构设置).select("$.BMBH").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.BMBH+','+e.BMMC) !==-1).
          forEach(e=>{
            var d = linq.from(机构设置).firstOrDefault(item=>item.BMBH == e.BMBH);
            if(d){
              e.BMMC = d.BMMC;
            }
          });
            resolve();
          }catch(e){
            reject(e);
          }
     }) 
    },
    async fetchData() {
      var user = getUser();
      var datas = await get机构设置(user.XXID,user.CJPC);

      return datas;
    },
      add_check(model,list){
      //
          //是否已经存在数据
          var first =  linq.from(list).firstOrDefault(e=>e.BMBH ==  model.BMBH);
          
          if(first)
          return  "已经存在对应编号的机构"
      //
    },
    onadd(model) {
      //
      var temp = JSON.parse(JSON.stringify(model));
      return new Promise(async (resolve, reject) => {
      try{
        
        console.log(JSON.stringify(temp));
              this.add_check()
        var model = await add机构设置(temp);
  
        resolve(model);
      }catch(e){
        reject(e);
      }
       
      });
    },
    onupdate(model) {
      //
      var temp = JSON.parse(JSON.stringify(model));
      return new Promise(async (resolve, reject) => {
      
        console.log(JSON.stringify(temp));
        var model = await update机构设置(temp);
        resolve(model);
      });
      //
    },
    ondelete(model) {
      var temp = JSON.parse(JSON.stringify(model));
      return new Promise(async (resolve, reject) => {
        ;
        await delete机构设置(temp);
        resolve();
      });
    }
  }
};
</script>
<style>
</style>

